package zy.data.food.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import zy.data.food.domain.ProductCategory;
import zy.data.food.domain.ProductInfo;
import zy.data.food.repository.ProductCategoryRepository;
import zy.data.food.repository.ProductIfoRepository;
import zy.data.food.service.ProductService;
import zy.data.food.util.ResultVOUtil;
import zy.data.food.vo.ProductInfoVO;
import zy.data.food.vo.ProductVO;
import zy.data.food.vo.ResultVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class testController {

    @Autowired
    private ProductCategoryRepository categoryRepository;

    @Autowired
    private ProductIfoRepository productIfoRepository;

    @Autowired
    private ProductService productService;

    @GetMapping(value = "/test")
    public List<ProductCategory> productCategoryList(){
        List<ProductCategory> productCategoryList = categoryRepository.findAll();
        return productCategoryList;
    }

    @GetMapping(value = "/test1")
    public List<ProductInfo> productInfoList(){
        List<ProductInfo> productInfoList = productIfoRepository.findAll();
        return productInfoList;
    }

    //测试productVO
    @GetMapping(value = "/test2")
    public ResultVO ResultList(){

        List<ProductCategory> productCategoryList = categoryRepository.findAll();
        List<ProductInfo> productInfoList = productIfoRepository.findAll();
        List<ProductVO> productVOList = new ArrayList<>();

        for (ProductCategory productCategory : productCategoryList){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(productCategory.getCategoryName());
            productVO.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for(ProductInfo productInfo : productInfoList){
                if(productInfo.getCategoryType()==productCategory.getCategoryType()) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);


            productVOList.add(productVO);
        }
//        ResultVO resultVO = new ResultVO();
//        resultVO.setCode(1);
//        resultVO.setMsg("成功");
//        resultVO.setData(productVOList);
//        return resultVO;
        return ResultVOUtil.success(productVOList);
    }

    @GetMapping(value = "/test3")
    public List<ProductInfo> CategoryList1(){

        List<ProductInfo> productInfoList = productService.findAll();
        return productInfoList;
        //查询所有类目
//            List<Integer> categoryTypeList = new ArrayList<>();
//
//            //传统写法
//            for(ProductInfo productInfo : productInfoList){
//                categoryTypeList.add(productInfo.getCategoryType());
//            }
        //新方法 (java8 lamda表达式)
//        List<Integer> categoryTypeList = productInfoList.stream()
//                .map(e -> e.getCategoryType())
//                .collect(Collectors.toList());
//        return categoryTypeList.size();
    }




}
